<?php
/**
 * Smarty plugin
 *
 * @package Smarty
 * @subpackage plugins
 */

/**
 * Google Analytics plugin
 * Generate XHTML 1.1 valid Google Analytics code
 * Name: google_analytics<br>
 * Date: 2009.09.17.<br>
 *
 * Examples:
 * <pre>
 * {google_analytics code=UA-xxxxxx-x domain=domain.com}
 * </pre>
 *
 * @author László Kovács <info@laszlokovacs.com>
 * @license http://www.gnu.org/copyleft/gpl.html GPL
 * @param array $params
 *        	code parameter required
 * @param Smarty $smarty
 * @return string
 */
function smarty_function_google_analytics($params, &$smarty){

	if (empty($params['code'])){
		$smarty->trigger_error("google_analytics: missing parameter");
		return;
	}

	$str = '<script type="text/javascript">
			var _gaq = _gaq || [];
			_gaq.push([\'_setAccount\', \'' . $params['code'] . '\']);
			_gaq.push([\'_setDomainName\', \'.' . $params['domain'] . '\']);
			_gaq.push([\'_trackPageview\']);
			(function() {
				var ga = document.createElement(\'script\'); ga.type = \'text/javascript\'; ga.async = true;
				ga.src = (\'https:\' == document.location.protocol ? \'https://ssl\' : \'http://www\') + \'.google-analytics.com/ga.js\';
				var s = document.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(ga, s);
			})();
			</script>
			';

	return $str;
}